const AllHttp = require("../../utils/AllHttp")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carts:[],
    sum:"",
    allChecked:""
  },

  onLoad:async function (options) {
    var Res = await AllHttp.cartHttp()
    console.log(Res.data);
    var allChecked = Res.data.every(item=>{
      return item.isSelected;
    })
    this.setData({
      carts:Res.data,
      allChecked
    })
    this.handleSum()
    
  },
  handleNum(event){
    var {aid} = event.currentTarget.dataset;
    var carts = this.data.carts;
    carts.forEach(item=>{
      if(item.id==aid){
        item.productCount = event.detail;
      }
    })
    this.setData({
      carts,
    })
    this.handleSum()

  },
  handleSelect(e){
    var carts = this.data.carts;
    var {aid} = e.currentTarget.dataset;
    carts.forEach(item=>{
      if(item.id==aid){
        item.isSelected = e.detail;
      }
    })
    var allChecked = carts.every(k=>{
      return k.isSelected;
    })
    this.setData({
      carts,
      allChecked
    })
    this.handleSum()
  },

  handleAll(e){
    var carts = this.data.carts;
    carts.forEach(item=>{
      item.isSelected = e.detail;
    })
    this.setData({
      carts,
      allChecked:e.detail,
    })
    this.handleSum()
  },
  handleSum(){
    var sum = 0;
    var carts = this.data.carts;
    carts.forEach(item=>{
      if(item.isSelected){
        sum += (item.productPrice)*(item.productCount)   
     }
    })
    console.log(sum);
    this.setData({
      sum:sum*100,
    })
  },
})



 



